//
//  KHHRecommendIndexViewCell.m
//  Travel
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "KHHRecommendIndexViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GIF.h"


@interface KHHRecommendIndexViewCell ()

@property (nonatomic, retain)UIImageView *imgGray;

@end

@implementation KHHRecommendIndexViewCell

//初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.bigimg = [[UIImageView alloc] init];
        [self.contentView addSubview:self.bigimg];
        self.bigimg.contentMode = UIViewContentModeScaleAspectFill;
        self.bigimg.clipsToBounds = YES;
        self.bigimg.layer.masksToBounds = YES;
        self.bigimg.layer.cornerRadius = 6;

        
        self.imgGray = [[UIImageView alloc] init];
        [self.contentView addSubview:self.imgGray];
        self.imgGray.frame = CGRectMake(0, 0, (self.frame.size.width - 20), 150);
        self.imgGray.image = [UIImage imageNamed:@"jiabian2"];
        self.imgGray.layer.masksToBounds = YES;
        self.imgGray.layer.cornerRadius = 6;
        
        self.titleLable = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLable];
        self.titleLable.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        self.titleLable.shadowColor = [UIColor grayColor];
        self.titleLable.textColor = [UIColor colorWithRed:0.99 green:0.98 blue:0.97 alpha:1];
        self.titleLable.numberOfLines = 2;
        
        self.blueLable = [[UILabel alloc] init];
        self.blueLable.backgroundColor = [UIColor colorWithRed:0.41 green:0.78 blue:0.84 alpha:1];
        self.blueLable.layer.masksToBounds = YES;
        self.blueLable.layer.cornerRadius = 2;
        [self.contentView addSubview:self.blueLable];
        
        self.beginDateLable = [[UILabel alloc] init];
        [self.contentView addSubview:self.beginDateLable];
        self.beginDateLable.shadowColor = [UIColor grayColor];
        self.beginDateLable.textColor = [UIColor colorWithRed:0.99 green:0.98 blue:0.97 alpha:1];
        self.beginDateLable.font = [UIFont systemFontOfSize:12];
        
        self.dayCountLable = [[UILabel alloc] init];
        [self.contentView addSubview:self.dayCountLable];
        self.dayCountLable.shadowColor = [UIColor grayColor];
        self.dayCountLable.textColor = [UIColor colorWithRed:0.99 green:0.98 blue:0.97 alpha:1];
        self.dayCountLable.font = [UIFont systemFontOfSize:12];
        
        self.placeLable = [[UILabel alloc] init];
        [self.contentView addSubview:self.placeLable];
        self.placeLable.shadowColor = [UIColor grayColor];
        self.placeLable.textColor = [UIColor colorWithRed:0.99 green:0.98 blue:0.97 alpha:1];
        self.placeLable.font = [UIFont systemFontOfSize:12];
        
        self.userPhotoImg = [[UIImageView alloc] init];
        [self.contentView addSubview:self.userPhotoImg];
        
        self.userNameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.userNameLabel];
        self.userNameLabel.shadowColor = [UIColor grayColor];
        self.userNameLabel.textColor = [UIColor colorWithRed:0.99 green:0.98 blue:0.97 alpha:1];
        self.userNameLabel.font = [UIFont systemFontOfSize:12];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //    //夜间模式
    //
    //    //接收通知中心
    //
    //    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    //
    //    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNight) name:@"ISNIGHT" object:user];
    
    //夜间模式
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isNight"]) {
        self.contentView.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1];
    }else
    {
        self.contentView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    }
    
    //背景图
    self.bigimg.frame = CGRectMake(10, 5, HHWIDTH - 20, HHHEIGHT - 10);
    CGSize BigimgSize = self.bigimg.frame.size;
    CGPoint BigimgPoint = self.bigimg.frame.origin;
    
    self.imgGray.frame = self.bigimg.frame;
    
    
    //自适应标题
    CGFloat HTitleLable = [[self class]heightWithText:self.titleLable.text fontSize:18 Width:BigimgSize.width - 20];
    self.titleLable.frame = CGRectMake(BigimgPoint.x + 10, BigimgPoint.y + 15, BigimgSize.width - 20, HTitleLable);
    
    
    
    //日期
    self.beginDateLable.frame = CGRectMake(BigimgPoint.x + 10 + 5 + 5 , self.titleLable.frame.origin.y + HTitleLable + 10, HHWIDTH / 4 , 15);
    //天数
    self.dayCountLable.frame = CGRectMake(self.beginDateLable.frame.origin.x + self.beginDateLable.frame.size.width + 10, self.beginDateLable.frame.origin.y, HHWIDTH / 3, self.beginDateLable.frame.size.height);
    //位置
    self.placeLable.frame = CGRectMake(self.beginDateLable.frame.origin.x, self.beginDateLable.frame.origin.y + 15 + 5, HHWIDTH /3, 15);
    //蓝条
    self.blueLable.frame = CGRectMake(self.titleLable.frame.origin.x, self.beginDateLable.frame.origin.y, 5, self.beginDateLable.frame.size.height + self.placeLable.frame.size.height + 5);
    //头像
    self.userPhotoImg.frame = CGRectMake(self.blueLable.frame.origin.x, self.blueLable.frame.origin.y + self.blueLable.frame.size.height + 200 / 4, BigimgSize.width / 7, BigimgSize.width / 7);
    self.userPhotoImg.layer.masksToBounds = YES;
    self.userPhotoImg.layer.cornerRadius = self.userPhotoImg.frame.size.width / 2;
    
    self.userNameLabel.frame = CGRectMake(self.userPhotoImg.frame.origin.x + self.userPhotoImg.frame.size.width + 5, self.userPhotoImg.frame.origin.y + 10, BigimgSize.width - 10 - self.userPhotoImg.frame.size.width - 5 - 150 , self.userPhotoImg.frame.size.width - 20);
    
}

- (void)setTravelModel:(KHHRecommendModel *)travelModel
{
    _travelModel = travelModel;
    
    [self.bigimg sd_setImageWithURL:[NSURL URLWithString:_travelModel.cover_image] placeholderImage:[UIImage sd_animatedGIFNamed:@"load"]];
    self.titleLable.text = _travelModel.name;
    self.beginDateLable.text = _travelModel.first_day;
    
    NSString *str = [NSString stringWithFormat:@"%@ %@", _travelModel.day_count, @"天"];
    self.dayCountLable.text = str;
    
    self.placeLable.text = _travelModel.popular_place_str;
    
}
- (void)setTravelUserModel:(KHHRecommendModel *)travelUserModel
{
    _travelUserModel = travelUserModel;
    
    [self.userPhotoImg sd_setImageWithURL:[NSURL URLWithString:travelUserModel.avatar_m] placeholderImage:nil];
    self.userNameLabel.text =[NSString stringWithFormat:@"%@ %@", @"by", travelUserModel.name];
}


//计算文本高度的方法
+ (CGFloat)heightWithText:(NSString *)text fontSize:(CGFloat)fontsize Width:(CGFloat)width

{
    //设置一个字典 保存文本属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontsize]};
    
    //预设尺寸 文本最大不会超过这个范围
    CGSize size = CGSizeMake(width, 2000);
    //根据文本信息 计算得到一个CGRect
    //参数1. 尺寸范围
    //参数2. 获取文本尺寸的方式
    //参数3. 文本属性
    //参数4. 无用信息(上下文)
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}





- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
